package networkSimulation.simulation.animals.statistic

import networkSimulation.statistic.{Statistic, VisualStatistics}
import networkSimulation.simulation.animals.framework._
import networkSimulation.framework.Graph

/**
 * Statistic representing the percentage of dead animals as a bar chart.
 *
 * @author  Tim Caljé
 *          Jeroen De Raedt
 *          Jorick Van Aken
 */
class PercentageDeadBar  [N<%Animal,E<%Hunt[N]] extends Statistic[N,E]{
  private var wolf = 0
  private var dinosaur = 0
  private var rabbit = 0
  private var wolfAmounts =List[Int]()
  private var dinosaurAmounts =List[Int]()
  private var rabbitAmounts =List[Int]()

  @Override
  override def generateStatistic(vis:VisualStatistics,path : String){
    var list = List[Int]()
    var strings = List[String]()
    strings::= "Wolfs"
    strings::= "Dinosaurs"
    strings::= "Rabbits"
    val wolfTemp = wolfAmounts.last - wolfAmounts.head
    list::=(wolfTemp*100/wolfAmounts.last)
    val dinosaurTemp = dinosaurAmounts.last - dinosaurAmounts.head
    list::= (dinosaurTemp*100/dinosaurAmounts.last)
    val rabbitTemp = rabbitAmounts.last - rabbitAmounts.head
    list::=(rabbitTemp*100/rabbitAmounts.last)
    vis.createBarChart("Percentage Dead","Amount",list,strings,"Nodes",path)
  }

  @Override
  override def updateStatistics(graph: Graph[N, E],k:String){
    wolf = 0
    dinosaur = 0
    rabbit = 0
    for(node: N <- graph.nodes){
      if(node.isInstanceOf[Wolf]){
        wolf+=1
      }
      if(node.isInstanceOf[Dinosaur]){
        dinosaur+=1
      }
      if(node.isInstanceOf[Rabbit]){
        rabbit+=1
      }
    }
    key = k
  }

  @Override
  override def loadData(data : xml.NodeSeq){
    wolfAmounts ::= (data \\ "Wolf").text.toInt
    dinosaurAmounts ::= (data \\ "Dinosaur").text.toInt
    rabbitAmounts ::= (data \\ "Rabbit").text.toInt
  }

  @Override
  override def toXml(): xml.NodeSeq  = {
    <Stat key={key}>
      <Wolf>{ wolf }</Wolf>
      <Dinosaur>{ dinosaur }</Dinosaur>
      <Rabbit>{ rabbit }</Rabbit>
    </Stat>
  }
}